package com.example.groupaimationdemo.slice;

import com.example.groupaimationdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {
    // 定义AnimatorValue实例对象
    private AnimatorValue animatorValue = new AnimatorValue();
    //创建属性动画对象
    private AnimatorProperty animatorProperty = new AnimatorProperty();
    //创建动画组对象
    private AnimatorGroup animatorGroup = new AnimatorGroup();
    private boolean started = false;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 获取模板中自动生成的Text组件实例
        Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        // 数值动画
        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                text.setText(String.format("倒计时：%.2f秒", 10 - (10 * v)));
            }
        });
        // 属性动画
        animatorProperty.setTarget(text);
        animatorProperty.rotate(360).alphaFrom(0).alpha(1).scaleXFrom(1).scaleX(2).scaleYFrom(1).scaleY(2);

        // 同时执行上面定义的数值动画和属性动画
        animatorGroup.runSerially(animatorValue, animatorProperty);
        // 设置动画无限循环
        animatorGroup.setLoopedCount(Animator.INFINITE);
        // 设置动画执行时长
        animatorGroup.setDuration(10000);

        //点击开始/停止动画
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!started) {
                    //启动动画组
                    animatorGroup.start();
                } else {
                    //停止动画组
                    animatorGroup.stop();
                }
                started = !started;
            }
        });
    }
}
